/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Pembelian;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import Koneksi.koneksi;
import MenuUtamaSistem.MenuUtamaController;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.Timer;

/**
 *
 * @author asus
 */
public class PembelianController {

    private Connection konek = null;
    private static PembelianController pembeliancontroller;

    public PembelianController(Connection konek) {
        this.konek = konek;
    }

    public static PembelianController getBarangKoneksi() throws SQLException, FileNotFoundException, IOException {
        koneksi kon = new koneksi();
        if (pembeliancontroller == null) {
            pembeliancontroller = new PembelianController(koneksi.getDBConnection());
        }
        return pembeliancontroller;
    }

    public static void showDateAndClock(final JLabel tanggalbeli, final JLabel jamLabel1) {
        /**
         *
         * @author Anonim, via search google.com
         */
//         */
        ActionListener taskPerformer = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                GregorianCalendar gc = new GregorianCalendar();
                SimpleDateFormat frmtDate = new SimpleDateFormat("dd-MMM-yyyy");

                String nol_jam = "", nol_menit = "", nol_detik = "";

                java.util.Date dateTime = new java.util.Date();
                int nilai_jam = dateTime.getHours();
                int nilai_menit = dateTime.getMinutes();
                int nilai_detik = dateTime.getSeconds();

                if (nilai_jam <= 9) {
                    nol_jam = "0";
                }
                if (nilai_menit <= 9) {
                    nol_menit = "0";
                }
                if (nilai_detik <= 9) {
                    nol_detik = "0";
                }

                String jam = nol_jam + Integer.toString(nilai_jam);
                String menit = nol_menit + Integer.toString(nilai_menit);
                String detik = nol_detik + Integer.toString(nilai_detik);

                jamLabel1.setText(jam + " : " + menit + " : " + detik + " WIB");
                tanggalbeli.setText(frmtDate.format(gc.getTime()));
            }
        };
        new Timer(1000, taskPerformer).start();
    }

    public int tambahPembelian(pembelian beli) throws SQLException {
        String kode = beli.getNo_transaksibeli();
        int stok = beli.getJumlah_beli();
        int hargabeli = beli.getHarga_pembelian();
        String waktu = beli.getWaktu();
        String id = beli.getIdUser();

        String sql = "{CALL spInsertPembelian(?,?,?,?,?,?)}";
        CallableStatement call = null;
        call = konek.prepareCall(sql);

        call.setString(1, kode);
        call.setInt(2, stok);
        call.setInt(3, hargabeli);
        call.setString(4, waktu);
        call.setString(5, id);
        call.registerOutParameter(6, java.sql.Types.INTEGER);
        call.execute();
        int status = call.getInt(6);
        call.close();
        konek.commit();
        return status;

    }

    public pembelian updatePembelian(pembelian beli) throws SQLException {
        CallableStatement callst = null;
        String sql = "{CALL UPDATENOMOR(?,?,?)}";
        callst = konek.prepareCall(sql);
        callst.setString(1, beli.getNo_transaksibeli());
        callst.setInt(2, beli.getJumlah_beli());
        callst.setInt(3, beli.getHarga_pembelian());
//        callst.execute();
//        String no= callst.getString(1);
//        int jumlah = callst.getInt(2);
//        int harga = callst.getInt(3);

//        beli.setNo_transaksibeli(no);
//        beli.setJumlah_beli();
//        beli.setHarga_pembelian(harga);
        callst.execute();
        callst.close();
        konek.commit();
        
        return beli;

    }
//    public String noPembelian(String kode) throws SQLException{
//        CallableStatement call=null;
//        String sql="{CALL noPembelian(?,?)}";
//        call=konek.prepareCall(sql);
//        call.setString(1, kode);
//        call.registerOutParameter(2, java.sql.Types.VARCHAR);
//        call.execute();
//        String nomor=call.getString(2);
//        call.close();
//        konek.commit();
//        return nomor;
//    }

    public pembelian cariNomor(pembelian beli) throws SQLException, Exception {
        CallableStatement callst = null;
        String sql = "{CALL CARINOMORBELI(?,?,?)}";
        callst = konek.prepareCall(sql);
        callst.setString(1, beli.getNo_transaksibeli());
        callst.registerOutParameter(2, java.sql.Types.INTEGER);
        callst.registerOutParameter(3, java.sql.Types.INTEGER);
        callst.execute();
        int jumlah = callst.getInt(2);
        int harga = callst.getInt(3);

        beli.setJumlah_beli(""+jumlah);
        beli.setHarga_pembelian(""+harga);
        callst.execute();
        callst.close();

        return beli;
    }

     public List<pembelian> detailNOmor() throws SQLException, Exception {
        PreparedStatement statement = null;
        ResultSet result = null;

        konek.setAutoCommit(false);
        String sql = "select waktu,notransaksipembelian from pembelianpulsa order by notransaksipembelian";
        statement = konek.prepareStatement(sql);
        result = statement.executeQuery();
        List<pembelian> List = new ArrayList<pembelian>();


        while (result.next()) {
//            Barang barang = new Barang();
            pembelian beli  = new pembelian();
            beli.setNo_transaksibeli(result.getString("notransaksipembelian"));
            beli.setWaktu(result.getString("waktu"));



//           ritelList.add(barang);
            List.add(beli);
        }
        konek.commit();
        return List;
    }
}
